package com.toe.business.webapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toe.business.service.BusinessInformationTempService;
import com.toe.business.webapi.mapper.BusinessInformationTempMapper;
import com.toe.common.exception.ToeServiceException;
import com.toe.common.restful.JsonPage;
import com.toe.common.restful.StateEnum;
import com.toe.pojo.dto.business.BusinessInformationTempDTO;
import com.toe.pojo.vo.business.BusinessInformationTempVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@DubboService
@Service
public class BusinessInformationTempServiceImpl implements BusinessInformationTempService {
    @Autowired
    BusinessInformationTempMapper businessInformationTempMapper;

    // 申请
    @Override
    public void addApplication(BusinessInformationTempDTO businessInformationTempDTO) {
        int row = businessInformationTempMapper.insertApplication(businessInformationTempDTO);
        if(row != 1) {
            throw new ToeServiceException(StateEnum.ERR_SERVER_BUSY,"服务器繁忙");
        }
    }

    // 分页查询申请表
    @Override
    public JsonPage<BusinessInformationTempVO> QueryApplication(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<BusinessInformationTempVO> list = businessInformationTempMapper.selectApplication();
        return JsonPage.restPage(new PageInfo<>(list));
    }

    // 审核不通过的商家修改is_handled字段为2
    @Override
    public void forbidApplication(Long id) {
        int rows = businessInformationTempMapper.forbidIsHandled(id);
        if(rows != 1) {
            throw new ToeServiceException(StateEnum.ERR_SERVER_BUSY,"服务器繁忙");
        }
    }

    // 审核通过的商家入驻
    @Override
    public void passApplication(Long id) {
        int tag = businessInformationTempMapper.passIsHandled(id);
        BusinessInformationTempVO pass = businessInformationTempMapper.selectPassApplication(id);
        int rows = businessInformationTempMapper.insertBusinessInformation(pass);
        if(tag != 1 && rows != 1) {
            throw new ToeServiceException(StateEnum.ERR_SERVER_BUSY,"服务器繁忙");
        }
    }
}
